﻿@{
    ViewBag.Title = "AddArrange";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    $(function () {
        gridList();
        init();

    });

    function init() {
        var date = new Date();
        //年份
        $("#Year").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                if (top.clients.yearArr && top.clients.yearArr.length) {
                    $.each(top.clients.yearArr, function () {
                        resultObjArr.push({ id: this, text: this });
                    });
                }
                return resultObjArr;
            },
            selectedValue: 1900 + date.getYear(),
        });
        //月份
        $("#Month").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                if (top.clients.monthArr && top.clients.monthArr.length) {
                    $.each(top.clients.monthArr, function () {
                        resultObjArr.push({ id: this, text: parseInt(this) < 10 ? "0" + this : this });
                    });
                }
                return resultObjArr;
            },
            selectedValue: date.getMonth() + 1,
        });
    }
    function gridList() {
        var lastRowId;
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/TherapeutistManage/TherapeutistCompleteList/GetAllRehabDoctor",
            shrinkToFit: true,
            height: $(window).height() - 130,
            colModel: [
                { label: "主键", name: "Id", hidden: true, key: true },
                { label: '姓名', name: 'Name', width: 150, align: 'left' },
                { label: '工号', name: 'gh', width: 150, align: 'left' },
                { label: '天数', name: 'ts', width: 90, editable: true, align: "left" }
            ],
            viewrecords: true,
            onSelectRow_page: function (rowid, status) {
                if (rowid && lastRowId && rowid !== lastRowId) {   //&&: 相当于true/fase(判断是否有值)
                    $gridList.saveRow(lastRowId, function (result) {
                        // return false;   //验证输入内容是否合法，取消保存返回false    //无效 等等修复
                    }); //先保存处于编辑状态的那一行    //function checksave
                }
                lastRowId = rowid;
                $gridList.jqGrid('editRow', rowid, true, null, function (result) {
                    return false;   //验证输入内容是否合法，取消保存返回false    //无效 等等修复
                }, null, null, function () {
                });
            },
            gridComplete: function () {
                //启用行内编辑框
                EnableInlineEditBox();
            }
        });
    }

    //启用行内编辑框
    function EnableInlineEditBox() {
        //Grid加载完成开启行内编辑   1.先获取所有行Id  2.遍历调用editRow方法
        var rowIds = $("#gridList").jqGrid('getDataIDs');
        for (var i = 0; i < rowIds.length; i++) {

            $("#gridList").jqGrid('editRow', rowIds[i], true, null, null, null, null, function (callbackRowId) {     //true: when set to true we can use [Enter] key to save the row and [Esc] to cancel editing.
            });
        }
    }

    function submitForm() {
        var year = $("#Year").val();
        var month = $("#Month").val();
        if (year == '-1' || month == '-1') {
            $.modalAlert("请选择年月", "warning");
            return;
        }
        var par = {};
        var dic = [];
        var rowIds = $("#gridList").jqGrid('getDataIDs');
        for (var i = 0; i < rowIds.length; i++) {
            //遍历使编辑框处于保存状态
            $("#gridList").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);
            var rowData = $("#gridList").jqGrid('getRowData', rowIds[i]);
            if (!rowData) {
                break;
            }
            if (!(/^(\+|-)?\d+$/.test(rowData.ts))) {
                $.modalAlert("工号（" + rowData.gh + "）天数不是数字，请确认。", 'warning');
                return;
            }
            if ((rowData.ts > 31)) {
                $.modalAlert("不能大于31天，请确认。", 'warning');
                return;
            }
        }
        var data = $("#gridList").jqGrid('getRowData_AllLine', undefined, false);
        var key = year + "," + month;
        dic.push({ 'key': key, 'value': data });
        par["vo"] = dic;
        $.najax({
            type: "POST",
            url: "/TherapeutistManage/TherapeutistCompleteList/AddRehabDoctorRange",
            data: par,
            dataType: "json",
            success: function () {
                $.loading(false);
                $.modalClose();
                $.modalMsg("操作成功", 'success');
            }
        })
    }

    function getSearchPostData() {
        var postData = {};
        postData.year = $('#Year').val();
        postData.month = $('#Month').val();

        if (!postData.year || !postData.month) {
            return;
        }
        return postData;
    }
</script>
<form>
    <div class="topPanel" style="border-bottom:1px solid rgb(221, 221, 221);margin-bottom: -1%;">
        <table class="form">
            <tr>
                <td class="formTitle"><span style="color:red">*</span>年份</td>
                <td class="formValue">
                    <select id="Year" class="form-control">
                        <option value="-1">==请选择==</option>
                    </select>
                </td>
                <td class="formTitle"><span style="color:red">*</span>月份：</td>
                <td class="formValue">
                    <select id="Month" class="form-control">
                        <option value="-1">==请选择==</option>
                    </select>
                </td>
                <td class="formValue"></td>
            </tr>
        </table>
    </div>
    <div id="formPatSer" class="gridPanel" style="margin-top:1%; margin-left:1%">
        <table id="gridList"></table>
    </div>
</form>

